I tried creating and deploying an AWS Lambda Functions using SAM CLI
Introduction
Hello, I’m Hemanth from the Alliance Department. In this blog, I will guide you through the process of building an AWS Lambda service using the AWS Serverless Application Model (SAM) CLI. This process is essential for developers looking to build, test, and deploy Lambda functions using SAM, which simplifies the entire development lifecycle for serverless applications.
AWS
Amazon Web Services, or AWS, is a cloud service platform that provides content distribution, database storage, processing capacity, and other features to support corporate expansion. AWS has offered a broad range of services in many different categories, including Compute, Storage, Networking, Database, Management Tools, and Security.
AWS Lambda
A serverless compute service which runs code as a reply to events and automatically takes care of the bottom resources. It runs code on high availability compute infrastructure and performs all the administration of the compute resources. A few examples are HTTP requests via Amazon API Gateway, changes to objects in S3, and many others.
AWS SAM
The AWS Serverless Application Model (SAM) is an open-source framework that simplifies the process of building and deploying serverless applications on AWS. SAM provides shorthand syntax to define AWS resources, making it easier to develop applications that use Lambda, API Gateway, DynamoDB, and more.
Demo
First, install the AWS Toolkit extension in Visual Studio Code. This tool simplifies AWS development and allows you to interact with AWS services directly from the IDE.
After installation, connect to AWS by selecting your AWS profile in the left-hand panel of VS Code. You'll be able to view and manage AWS services directly from the editor.
To create a new SAM project, open the command palette by pressing Cmd + Shift + P or Ctrl + Shift + P on Windows, and search for "AWS: Create Lambda SAM Application."
For this demo, we will select the following settings:
Runtime - nodejs18.x, architecture - x86_64 Then, select the AWS SAM Hello World template, which provides a basic Lambda project structure.
First sample hello-world project has been successfully created.
Once your project is created, run the build command to generate the build artifacts. This step prepares your code and dependencies for deployment.
Next, deploy your Lambda function using the SAM CLI command sam deploy. This command uploads your build artifacts to AWS and creates the necessary resources such as the Lambda function, API Gateway, and IAM roles.
After deployment, navigate to the AWS Management Console to verify that your Lambda function has been successfully created.
Finally, check the API Gateway to ensure it has been properly linked to your Lambda function.
Once the setup is complete, you have a fully functioning serverless service built using AWS Lambda and SAM CLI.
Cleaning up the resources
sam delete --stack-name sam-lambda
Conclusion
By following this guide, you have successfully built, tested, and deployed an AWS Lambda service using AWS SAM CLI. This approach simplifies the development and deployment of serverless applications while providing a robust, scalable, and efficient infrastructure. With SAM's local testing capabilities and seamless integration with AWS services, it's a powerful tool for developers looking to build serverless solutions at scale.